package cn.group.hotel2.service.impl;

import cn.group.hotel2.ex.ServiceException;
import cn.group.hotel2.mapper.PermissionMapper;
import cn.group.hotel2.pojo.entity.PermissionTreeEntity;
import cn.group.hotel2.pojo.vo.PermissionListItemVO;
import cn.group.hotel2.pojo.vo.PermissionTreeVO;
import cn.group.hotel2.service.IPermissionService;
import cn.group.hotel2.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理权限业务的实现类
 *
 * @author Edgar
 * @version 1.0.0
 * @date 2022/11/30
 */
@Slf4j
@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    PermissionMapper permissionMapper;
    @Override
    public PermissionTreeVO permissionTree() {
        List<PermissionListItemVO> permissionListItemVOs = permissionMapper.list();
        PermissionTreeVO<PermissionTreeEntity> treeVO = initTree(permissionListItemVOs);
        return treeVO;
    }

    /**
     * 初始化树
     *
     * @param permissionListItemVOs 权限列表项vos
     * @return {@link PermissionTreeVO}<{@link PermissionTreeEntity}>
     */
    private PermissionTreeVO<PermissionTreeEntity> initTree(List<PermissionListItemVO> permissionListItemVOs) {
        //TODO 缓存到Redis
        Map<Long,List<PermissionTreeEntity>> maps = new HashMap<>();
        for (PermissionListItemVO permissionListItemVO : permissionListItemVOs) {
            PermissionTreeEntity permissionTreeEntity = new PermissionTreeEntity();
            BeanUtils.copyProperties(permissionListItemVO,permissionTreeEntity);
            if (maps.containsKey(permissionTreeEntity.getParentNodeId())){
                List<PermissionTreeEntity> permissionTreeEntities = maps.get(permissionTreeEntity.getParentNodeId());
                permissionTreeEntities.add(permissionTreeEntity);
                continue;
            }
            ArrayList<PermissionTreeEntity> permissionTreeEntities = new ArrayList<>();
            permissionTreeEntities.add(permissionTreeEntity);
            maps.put(permissionTreeEntity.getParentNodeId(),permissionTreeEntities);
        }
        System.out.println(maps);
        List<PermissionTreeEntity> firstLevels = maps.get(0L);
        if (firstLevels==null || firstLevels.isEmpty()){
            String message = "没有一级分类对象";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        for (PermissionTreeEntity firstLevel : firstLevels) {
            List<PermissionTreeEntity> secondLevels = maps.get(firstLevel.getId());
            if (secondLevels==null||secondLevels.isEmpty()){
                log.warn("次分类没有二级分类");
                continue;
            }
           firstLevel.setChildren(secondLevels);
        }
        PermissionTreeVO<PermissionTreeEntity> treeVO = new PermissionTreeVO<>();
        treeVO.setPermissions(firstLevels);
        return treeVO;
    }
}
